欧美一区2区三区4区公司二百,国产精品婷婷午夜在线观看,自拍偷拍亚洲精品,国产美女诱惑一区二区

查看數(shù)組的大小

查看數(shù)組大小

成功擴容為20大小的數(shù)組。

實現(xiàn)獲取pos位置元素和順序表的長度,以及設置元素這三個接口。由于獲取和設置元素都需要檢查下標是否合法。所以我們寫一個判斷位置是否合法的方法。我們在判斷非法的時候我們直接輸出非法不合適,我們應該建立一個異常,然后拋出異常.

建立異常:

public class PosOutBoundsException extends RuntimeException{
? ? public PosOutBoundsException() {
? ? }
? ? public PosOutBoundsException(String message) {
? ? ? ? super(message);
? ? }
}

接口的實現(xiàn):

// 獲取 pos 位置的元素
public int get(int pos) ?{
? ? if (!checkPos(pos)){
? ? ? ? throw new RuntimeException("get 元素位置錯誤");
? ? }
? ? return this.elem[pos];
}
// 獲取順序表長度
public int size() {
? ? return this.usedSize;
}
// 給 pos 位置的元素設為 value【更新的意思 】
public void set(int pos, int value) {
? ? if (!checkPos(pos)){
? ? ? ? throw new RuntimeException("set 元素位置錯誤");
? ? }
? ? this.elem[pos] = value;
}
//檢查pos下表是否 合法
private boolean checkPos(int pos){
? ? if (pos < 0 || pos >= this.usedSize) return false;
? ? return true;
}

進行新增數(shù)據(jù)的測試 :

public static void main(String[] args) {
? ? SeqList seqList = new SeqList();
? ? seqList.add(1);
? ? seqList.add(2);
? ? seqList.add(3);
? ? seqList.add(4);
? ? seqList.add(5);
? ? seqList.add(6);
? ? System.out.println(seqList.get(3));
? ? seqList.display();
? ? seqList.set(3,1000);
? ? seqList.display();
? ? seqList.get(10);//獲取元素位置錯誤拋出異常。
}

最后輸出結(jié)果:

? ? 4
? ? 1 2 3 4 5 6
? ? 1 2 3 1000 5 6
? ? Exception in thread “main” java.lang.RuntimeException: get 元素位置錯誤
? ? at demoList2.SeqList.get(SeqList.java:56)
? ? at demoList2.Test.main(Test.java:17)

文章鏈接: http://m.qzkangyuan.com/22408.html

文章標題:查看數(shù)組的大小

文章版權(quán):夢飛科技所發(fā)布的內(nèi)容,部分為原創(chuàng)文章,轉(zhuǎn)載請注明來源,網(wǎng)絡轉(zhuǎn)載文章如有侵權(quán)請聯(lián)系我們!

聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
建站教程

數(shù)據(jù)結(jié)構(gòu)順序表

2023-7-25 13:23:39

建站教程

ArrayList的構(gòu)造

2023-7-26 14:26:03

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索
主站蜘蛛池模板: 新巴尔虎右旗| 鸡西市| 蒙阴县| 鄂尔多斯市| 丹东市| 寿宁县| 诏安县| 申扎县| 尤溪县| 陆丰市| 盐池县| 东丽区| 无为县| 页游| 民权县| 喜德县| 平远县| 杭锦旗| 高尔夫| 乐安县| 肃北| 瑞金市| 扶余县| 自贡市| 洛隆县| 嘉兴市| 栾城县| 垣曲县| 班戈县| 永寿县| 江陵县| 巴中市| 建宁县| 鹤岗市| 岐山县| 西和县| 时尚| 桐梓县| 彩票| 绥江县| 石林|